package com.allwees.bs.c.module.order.repository;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.allwees.bs.c.module.base.repository.BaseRepository;
import com.allwees.bs.c.module.order.entity.OrderItemEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Set;

public interface OrderItemRepository extends BaseRepository<OrderItemEntity, Integer> {
	@Query(value = "SELECT oi.product_uuid from o_order_item oi " + "INNER JOIN o_order o on oi.order_uuid = o.uuid "
			+ "WHERE o.user_uuid = ?1 AND oi.product_uuid in ?2 "
			+ "AND o.state != 'ORDER_STATE_CANCELED' AND o.state != 'ORDER_STATE_CLOSED'", nativeQuery = true)
	Set<String> findByUserAndProduct(@Param("userUuid") String userUuid, @Param("productUuids") Set<String> productUuids);

	@Query("SELECT oi from OrderItemEntity oi WHERE oi.orderUuid=:orderUuid")
	List<OrderItemEntity> findOrderItems(@Param("orderUuid") String orderUuid);

	List<OrderItemEntity> findByOrderUuidIn(Set<String> orderUuids);

	@Query("SELECT COUNT(ID) from OrderItemEntity oi WHERE oi.orderUuid =:orderUuid AND oi.reviewAt is null " +
			"AND oi.refunded = false AND oi.canceled = false")
	Integer getUnreviewItemCount(@Param("orderUuid") String orderUuid);

    OrderItemEntity getByUuid(String uuid);

	List<OrderItemEntity> findAllByUserUuid(String userUuid);

	List<OrderItemEntity> findAllByOrderUuid(String orderUuid);

	OrderItemEntity findByUuid(String orderItemUuid);
}
